yjx.controller('WxController', ['$rootScope', '$scope', '$state', '$compile', 'ipCookie', '$location', '$state', '$timeout', '$fetch',  'LoginService', 'modalService', '$locals','wxShareService','urlCgService',
    function WxController($rootScope, $scope, $state, $compile, ipCookie, $location, $state, $timeout, $fetch, LoginService, modalService, $locals,wxShareService,urlCgService) {
        //console.info(JSON.stringify($location)+'this is WxController');
        var wxState = $location.search().state
        var code = $location.search().code;
        $location.path($location.path()).search('');
        $locals.remove('wxState');
        if (code) {
            //alert(code);
            //alert($location.absUrl());
            //微信登录code传输x
            var wxLoginApi = '/weChat/callback'
            //var wxLoginApi = 'http://wanglan.free.ngrok.cc/cms/weChat/callback'
            //var wxLoginApi = 'http://www.wanglan.site/cms/weChat/callback'
            //$locals.set('wxState', wxState);//回调跳转页面

            $fetch.fetch(
                wxLoginApi,
                {
                    params: {
                        code: code,
                    },
                    catch:false
                }
            ).then(function (data) {
                    //alert($location.absUrl());
                    LoginService.initLoginData(data);
                    //alert("wxSate============="+wxState);
                    $locals.set('wxState', wxState);

                    //alert(wxState)
                    if(wxState == 'http://www.genetuan.com/' || wxState == 'http://www.genetuan.com' || wxState == 'http://www.genetuan.com/#' ||wxState == 'http://www.genetuan.com/#/'){
                        /*首页登录去进行身份选择*/
                        $timeout(function () {
                            $scope.tempwx = "components/user/identity/identity.html";
                            var html = '<div class="e-detail" ng-include="\'' + $scope.tempwx + '\'"></div>';
                            var detailHtml = $compile(html)($scope);
                            angular.element(document.querySelector('#m-detail')).append(detailHtml);//后排插入
                            angular.element(document.querySelector('#m-detail')).show();
                            $rootScope._dCount++;
                        })
                    }else{
                        //接口返回200后将此时的用户状态保存到rootScope或cookie
                        /*其他路径进行默认消费者身份*/
                        $rootScope.identity = 0;
                        //var userData = ipCookie('user');
                        //ipCookie('user', userData, ipCookiesParam);
                        ipCookie('identity',data.identity,ipCookiesParam);
                        //页面跳转解析url
                        //window.location.href = "http://www.genetuan.com/#/home"
                        var absurl = wxState.split('://')[1]
                        $timeout(function () {
                            if ((absurl && absurl.split('/#/') && absurl.split('/#/').length > 1) || (absurl && absurl.split('/') && absurl.split('/').length > 1)) {
                                urlCgService.encodeUrl(absurl);
                            } else {
                                $state.go("index");
                                //window.location.href = "http://www.genetuan.com/#/home"
                            }
                        },1000)
                    }

                }).catch(function (e) {
                    $location.path($location.path()).search('');
                    //$state.go('home');
                    modalService.mainMsgError(e.msg);
                });
        }

    }]);